﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.Collections;
using System.Collections.Specialized;
using Sample.PhotoBrowser.Components;

namespace Sample.PhotoBrowser
{
    /// <summary>
    /// Interaction logic for VBarChart.xaml
    /// </summary>
    public partial class FlickrPhotoBarChart
    {
        public FlickrPhotoBarChart()
        {
            InitializeComponent();
            Bars = new ObservableCollection<Bar>();
            ctrlBarChart.ItemsSource = Bars;
        }

        private readonly List<Color> _brushes = new List<Color> { Colors.Purple, Colors.YellowGreen, Colors.Magenta, Colors.DodgerBlue, Colors.LimeGreen, Colors.HotPink, Colors.Gold, Colors.AliceBlue, Colors.Turquoise, Colors.Yellow, Colors.Silver, Colors.MediumSpringGreen };

        public ObservableCollection<Bar> Bars
        {
            get;
            set;
        }

        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(FlickrPhotoBarChart), new UIPropertyMetadata(OnItemsSourceChanged));

        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            var photoBarChart = d as FlickrPhotoBarChart;
            if (photoBarChart != null) photoBarChart.RegisterForChanges();
        }

        private void RegisterForChanges()
        {
            var notifyCollectionChanged = ItemsSource as INotifyCollectionChanged;
            if (notifyCollectionChanged != null)
                notifyCollectionChanged.CollectionChanged += OnCollectionChanged;
        }

        void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Bars.Clear();
            var bars = new Dictionary<string, double>();

            foreach (FlickrPhoto p in ItemsSource)
            {
                var key = p.UserName;

                if (bars.ContainsKey(key))
                    bars[key] += 40;
                else
                    bars.Add(key, 40);
            }
            var i = 0;
            foreach (var key in bars.Keys)
            {
                var brush = i < _brushes.Count ? _brushes[i] : Colors.Purple;
                Bars.Add(new Bar(bars[key], key, brush));
                i++;
            }
        }
    }

    public class BarChartConverter: IValueConverter
    {
        #region IValueConverter Members

        public List<Color> Brushes = new List<Color> { Colors.Purple, Colors.YellowGreen, Colors.Magenta, Colors.DodgerBlue, Colors.LimeGreen, Colors.HotPink, Colors.Gold, Colors.AliceBlue, Colors.Turquoise, Colors.Yellow, Colors.Silver, Colors.MediumSpringGreen }; 

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                var bars = new Dictionary<string, double>();
                var returnCollection = new ObservableCollection<Bar>();

                foreach (FlickrPhoto photo in (ObservableCollection<object>)value)
                {
                    string key = photo.UserName;

                    if (bars.ContainsKey(key))
                        bars[key] += 40;
                    else
                        bars.Add(key, 40);
                }

                var i = 0;
                foreach (var key in bars.Keys)
                {
                    var brush = i < Brushes.Count ? Brushes[i] : Colors.Purple;
                    returnCollection.Add(new Bar(bars[key], key, brush));
                    i++;
                }

                return returnCollection;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class Bar: DependencyObject
    {
        public double BarWidth
        {
            get { return (double)GetValue(BarWidthProperty); }
            set { SetValue(BarWidthProperty, value); }
        }

        public string BarName
        {
            get { return (string)GetValue(BarNameProperty); }
            set { SetValue(BarNameProperty, value); }
        }

        public Color BarColor
        {
            get { return (Color)GetValue(BarColorProperty); }
            set { SetValue(BarColorProperty, value); }
        }

        public static readonly DependencyProperty BarColorProperty =
            DependencyProperty.Register("BarColor", typeof(Color), typeof(Bar), new UIPropertyMetadata(Colors.Purple));

        public static readonly DependencyProperty BarNameProperty =
            DependencyProperty.Register("BarName", typeof(string), typeof(Bar), new UIPropertyMetadata(""));

        public static readonly DependencyProperty BarWidthProperty =
            DependencyProperty.Register("BarWidth", typeof(double), typeof(Bar), new UIPropertyMetadata(0.0));

        public Bar(double width, string name, Color brush)
        {
            BarWidth = width;
            BarName = name;
            BarColor = brush;
        }
    }
}
